package com.stockz.equity.charting.painter.shape;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.HashSet;
import java.util.Set;

import org.springframework.stereotype.Component;

import com.stockz.equity.charting.model.Line;
import com.stockz.equity.charting.util.SwingUtil;
import com.stockz.equity.charting.view.StockChart;
import com.stockz.equity.charting.view.StockChartPanelSkin;
import com.stockz.equity.charting.view.StockChartPanelState;

@Component
public class LinePainter implements ShapePainter<Line,StockChart> {

	@SuppressWarnings({ "rawtypes" })
	private Set<Class> classSet = new HashSet<Class>(1);
	
	public LinePainter(){
		classSet.add(Line.class);
	}
	
	@Override
	public void paint(Graphics2D g, Line shape,StockChart view, Rectangle r) {
		StockChartPanelState state = view.getStockChartPanel().getStockChartPanelState();
		StockChartPanelSkin skin = view.getStockChartPanel().getStockChartPanelSkin();
		
		Point start = SwingUtil.fitPoint(state, shape.getStart());
		Point end = SwingUtil.fitPoint(state, shape.getEnd());
		r = state.getPriceRectangle();
		g.setClip(r);
		if(start.x == Integer.MAX_VALUE 
				|| start.x == Integer.MIN_VALUE
				|| start.y == Integer.MAX_VALUE
				|| start.y == Integer.MIN_VALUE){
			SwingUtil.restrictPointOutside(start, r);			
		}
		
		if(end.x == Integer.MAX_VALUE 
				|| end.x == Integer.MIN_VALUE
				|| end.y == Integer.MAX_VALUE
				|| end.y == Integer.MIN_VALUE){
			SwingUtil.restrictPointOutside(end, r);			
		}
		
		g.setColor(skin.getDrawingColor());
		g.drawLine(start.x, start.y, end.x, end.y);
		g.setClip(null);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public boolean supports(Class c){
		for(Class s : classSet){
			if(s.isAssignableFrom(c)){
				return true;
			}
		}
		return false;
	}
}
